﻿using MVCF.Features;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace SZ.Mech
{
    public class CompProperties_ChangeWeapon : CompProperties
    {
        public CompProperties_ChangeWeapon()
        {
            this.compClass = typeof(CompChangeWeapon);
        }

        public string toggleLabelKey;
        public string toggleDescKey;
        //public string toggleIconPath;

        public ThingDef ChangeWeaponA;
        public ThingDef ChangeWeaponB;
        public string iconPath;
    }
    public class CompChangeWeapon : ThingComp
    {
        public Texture2D Icon;

        public CompProperties_ChangeWeapon Props
        {
            get
            {
                return (CompProperties_ChangeWeapon)this.props;
            }
        }

        public override IEnumerable<Gizmo> CompGetGizmosExtra()
        {
            if(Icon == null)
            {
                Icon = ContentFinder<Texture2D>.Get(Props.iconPath);
            }

            Pawn pawn = this.parent as Pawn;
            if (pawn.Faction == Faction.OfPlayer)
            {
                //if (pawn.equipment.Primary.def == Props.ChangeWeaponA)
                //{
                //    icon = Props.ChangeWeaponB.uiIcon;
                //    //icon = ContentFinder<Texture2D>.Get(this.Props.toggleIconPath, true);
                //}
                //else if (pawn.equipment.Primary.def == Props.ChangeWeaponB)
                //{
                //    icon = Props.ChangeWeaponA.uiIcon;
                //}
                Command_Action changeButton = new Command_Action
                {
                    Order = 10000,
                    defaultLabel = this.Props.toggleLabelKey.Translate(),
                    defaultDesc = this.Props.toggleDescKey.Translate(),
                    icon = Icon,
                    //icon = ContentFinder<Texture2D>.Get(this.Props.toggleIconPath, true),
                    action = delegate ()
                    {
                        if (!pawn.Dead && !pawn.Downed && !pawn.mindState.mentalStateHandler.InMentalState && pawn.Faction == Faction.OfPlayer && pawn.GetOverseer() != null)
                        {
                            if (pawn.equipment?.Primary != null)
                            {
                                if(pawn.equipment.Primary.def == Props.ChangeWeaponA)
                                {
                                    ThingWithComps equip = (ThingWithComps)ThingMaker.MakeThing(Props.ChangeWeaponB, null);
                                    pawn.equipment.Remove(pawn.equipment.Primary);
                                    pawn.equipment.AddEquipment(equip);
                                }
                                else if(pawn.equipment.Primary.def == Props.ChangeWeaponB)
                                {
                                    ThingWithComps equip = (ThingWithComps)ThingMaker.MakeThing(Props.ChangeWeaponA, null);
                                    pawn.equipment.Remove(pawn.equipment.Primary);
                                    pawn.equipment.AddEquipment(equip);
                                }
                                else
                                {
                                    ThingWithComps equip = (ThingWithComps)ThingMaker.MakeThing(Props.ChangeWeaponA, null);
                                    pawn.equipment.Remove(pawn.equipment.Primary);
                                    pawn.equipment.AddEquipment(equip);
                                }
                            }
                        }
                    },
                    Disabled = pawn.Dead || pawn.Downed || pawn.mindState.mentalStateHandler.InMentalState || pawn.Faction != Faction.OfPlayer || pawn.GetOverseer() == null,
                };
                yield return changeButton;
            }
            yield break;
        }
    }
}

